import { IFeidaoAiBrowserComponent } from '@dfeidao/atom-web/interfaces';

import error from '@dfeidao/atom-web/msgbox/error';

import fire from '@dfeidao/atom-web/msg/fire';

import get from '@dfeidao/atom-web/local/get';

import gett from '@dfeidao/atom-web/global/get';
import success from '@dfeidao/atom-web/msgbox/success';

import nodejs from '@dfeidao/atom-web/msg/nodejs';
import { productid } from '../../atom/config';

import set_node_cls from '@dfeidao/atom-web/ui/set-node-cls';

export default async function a005(fd: IFeidaoAiBrowserComponent) {
	// todo
	// 保存
	const dta = gett('dta');
	const staff_no = dta.staff_no;
	const staff_name = dta.staff_name;
	const knowledge_name1 = fd.data.node.querySelector<HTMLInputElement>('[data-id="knowledge_name"]');
	const knowledge_name2 = knowledge_name1.value;
	const knowledge_explain1 = fd.data.node.querySelector<HTMLInputElement>('[data-id="knowledge_explain"]');
	const knowledge_explain2 = knowledge_explain1.value;

	const knowledge_class1 = get(fd, 'knowledge_class');
	const create_time1 = new Date().valueOf();

	const res2 = await nodejs<{ res: { code: 1; result: string } }>('knowledge-base/zj-000005/s002', {});
	const knowledge_no1 = res2.res.result;
	const res = await nodejs<{ code: 0 | 1; result: string }>('knowledge-base/zj-000005/s001', {
		filter: {
			productid,
			knowledge_no: knowledge_no1,
			knowledge_class: knowledge_class1,
			knowledge_name: knowledge_name2,
			knowledge_explain: knowledge_explain2,
			create_time: create_time1,
			creater_no: staff_no,
			creater: staff_name
		}
	});
	if (!knowledge_name2 || !knowledge_explain2) {
		alert("请填写相关数据");
		return;
	}
	if (res.code === 1) {
		success('新增成功');
		// tslint:disable-next-line: no-shadowed-variable
		const knowledge_name1 = fd.data.node.querySelector<HTMLInputElement>('[data-id="knowledge_name"]');
		knowledge_name1.value = "";
		// tslint:disable-next-line: no-shadowed-variable
		const knowledge_explain1 = fd.data.node.querySelector<HTMLInputElement>('[data-id="knowledge_explain"]');
		knowledge_explain1.value = " ";
		const del = fd.data.node.querySelector<HTMLDivElement>('[data-id="addpage"]');
		set_node_cls(del, 'display-n', true);
		if (knowledge_class1 === 2) {
			fire('zj-000001', 'a007');
		} if (knowledge_class1 === 3) {
			fire('zj-000001', 'a009');
		} if (knowledge_class1 === 4) {
			fire('zj-000001', 'a011');
		}

	} else {
		error('新增失败');
	}
}
